package com.yunban.project.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunban.project.message.domain.CSMessage;
import com.yunban.project.message.dto.CSMessageReq;
import com.yunban.project.message.enums.MessageType;
import com.yunban.project.message.mapper.CSMessageMapper;
import com.yunban.project.message.resp.CSMessageResp;
import com.yunban.project.message.service.CSMessageService;
import com.yunban.project.message.utils.WorkNoticeSendUtils;
import jakarta.annotation.Resource;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: lijiajun
 * @date: 2025-03-07
 * @version: 1.0
 */
@Service
public class CSMessageServiceImpl extends ServiceImpl<CSMessageMapper, CSMessage> implements CSMessageService {

    @Resource
    private WorkNoticeSendUtils workNoticeSendUtils;

    @Override
    public Boolean send(CSMessageReq req) {
        CSMessage csMessage = new CSMessage();
        BeanUtils.copyProperties(req, csMessage);
        csMessage.setType(MessageType.USER);
        this.save(csMessage);
        this.workNoticeSendUtils.sendWordNotice(req.getSessionId(), req.getMessage());
        return true;
    }

    @Override
    public Boolean reply(CSMessageReq req) {
        CSMessage csMessage = new CSMessage();
        BeanUtils.copyProperties(req, csMessage);
        csMessage.setType(MessageType.MANAGER);
        return this.save(csMessage);
    }

    @Override
    public List<CSMessageResp> findBySessionId(String sessionId) {
        // 查询这个sessionId的所有聊天信息
        LambdaQueryWrapper<CSMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CSMessage::getSessionId, sessionId);
        queryWrapper.orderByAsc(CSMessage::getId);
        return SmartPageUtil.convert(this.list(queryWrapper), CSMessageResp.class);
    }
}
